home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Term / Cap.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  9.3 KB  |  347 lines

  1. package Term::Cap;
  2. use Carp;
  3.  
  4.  
  5.  
  6. =head1 NAME
  7.  
  8. Term::Cap - Perl termcap interface
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.     require Term::Cap;
  13.     $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
  14.     $terminal->Trequire(qw/ce ku kd/);
  15.     $terminal->Tgoto('cm', $col, $row, $FH);
  16.     $terminal->Tputs('dl', $count, $FH);
  17.     $terminal->Tpad($string, $count, $FH);
  18.  
  19. =head1 DESCRIPTION
  20.  
  21. These are low-level functions to extract and use capabilities from
  22. a terminal capability (termcap) database.
  23.  
  24. The B<Tgetent> function extracts the entry of the specified terminal
  25. type I<TERM> (defaults to the environment variable I<TERM>) from the
  26. database.
  27.  
  28. It will look in the environment for a I<TERMCAP> variable.  If
  29. found, and the value does not begin with a slash, and the terminal
  30. type name is the same as the environment string I<TERM>, the
  31. I<TERMCAP> string is used instead of reading a termcap file.  If
  32. it does begin with a slash, the string is used as a path name of
  33. the termcap file to search.  If I<TERMCAP> does not begin with a
  34. slash and name is different from I<TERM>, B<Tgetent> searches the
  35. files F<$HOME/.termcap>, F</etc/termcap>, and F</usr/share/misc/termcap>,
  36. in that order, unless the environment variable I<TERMPATH> exists,
  37. in which case it specifies a list of file pathnames (separated by
  38. spaces or colons) to be searched B<instead>.  Whenever multiple
  39. files are searched and a tc field occurs in the requested entry,
  40. the entry it names must be found in the same file or one of the
  41. succeeding files.  If there is a C<:tc=...:> in the I<TERMCAP>
  42. environment variable string it will continue the search in the
  43. files as above.
  44.  
  45. I<OSPEED> is the terminal output bit rate (often mistakenly called
  46. the baud rate).  I<OSPEED> can be specified as either a POSIX
  47. termios/SYSV termio speeds (where 9600 equals 9600) or an old
  48. BSD-style speeds (where 13 equals 9600).
  49.  
  50. B<Tgetent> returns a blessed object reference which the user can
  51. then use to send the control strings to the terminal using B<Tputs>
  52. and B<Tgoto>.  It calls C<croak> on failure.
  53.  
  54. B<Tgoto> decodes a cursor addressing string with the given parameters.
  55.  
  56. The output strings for B<Tputs> are cached for counts of 1 for performance.
  57. B<Tgoto> and B<Tpad> do not cache.  C<$self-E<gt>{_xx}> is the raw termcap
  58. data and C<$self-E<gt>{xx}> is the cached version.
  59.  
  60.     print $terminal->Tpad($self->{_xx}, 1);
  61.  
  62. B<Tgoto>, B<Tputs>, and B<Tpad> return the string and will also
  63. output the string to $FH if specified.
  64.  
  65. The extracted termcap entry is available in the object
  66. as C<$self-E<gt>{TERMCAP}>.
  67.  
  68. =head1 EXAMPLES
  69.  
  70.     require POSIX;
  71.     my $termios = new POSIX::Termios;
  72.     $termios->getattr;
  73.     my $ospeed = $termios->getospeed;
  74.  
  75.  
  76.     $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
  77.  
  78.     $terminal->Trequire(qw/ce ku kd/);
  79.  
  80.  
  81.     $terminal->Tgoto('cm', $col, $row, $FH);
  82.  
  83.     $terminal->Tputs('dl', $count = 1, $FH);
  84.  
  85. =cut
  86.  
  87. sub termcap_path { ## private
  88.     my @termcap_path;
  89.     push(@termcap_path, $ENV{TERMCAP})
  90.     if ((exists $ENV{TERMCAP}) &&
  91.         (($^O eq 'os2' || $^O eq 'MSWin32')
  92.          ? $ENV{TERMCAP} =~ /^[a-z]:[\\\/]/i
  93.          : $ENV{TERMCAP} =~ /^\//));
  94.     if ((exists $ENV{TERMPATH}) && ($ENV{TERMPATH})) {
  95.     push(@termcap_path, split(/(:|\s+)/, $ENV{TERMPATH}))
  96.     }
  97.     else {
  98.     push(@termcap_path,
  99.         $ENV{'HOME'} . '/.termcap',
  100.         '/etc/termcap',
  101.         '/usr/share/misc/termcap',
  102.     );
  103.     }
  104.     grep(-f, @termcap_path);
  105. }
  106.  
  107. sub Tgetent { ## public -- static method
  108.     my $class = shift;
  109.     my $self = bless shift, $class;
  110.     my($term,$cap,$search,$field,$max,$tmp_term,$TERMCAP);
  111.     local($termpat,$state,$first,$entry);    # used inside eval
  112.     local $_;
  113.  
  114.     if (! $self->{OSPEED}) {
  115.     carp "OSPEED was not set, defaulting to 9600";
  116.     $self->{OSPEED} = 9600;
  117.     }
  118.     if ($self->{OSPEED} < 16) {
  119.     my @pad = (0,200,133.3,90.9,74.3,66.7,50,33.3,16.7,8.3,5.5,4.1,2,1,.5,.2);
  120.     $self->{PADDING} = $pad[$self->{OSPEED}];
  121.     }
  122.     else {
  123.     $self->{PADDING} = 10000 / $self->{OSPEED};
  124.     }
  125.  
  126.     $self->{TERM} = ($self->{TERM} || $ENV{TERM} || croak "TERM not set");
  127.     $term = $self->{TERM};    # $term is the term type we are looking for
  128.  
  129.     $tmp_term = $self->{TERM};
  130.     $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g;
  131.  
  132.     my $foo = (exists $ENV{TERMCAP} ? $ENV{TERMCAP} : '');
  133.  
  134.     if (($foo !~ m:^/:) && ($foo =~ m/(^|\|)${termpat}[:|]/)) {
  135.     $entry = $foo;
  136.     }
  137.  
  138.     my @termcap_path = termcap_path;
  139.     croak "Can't find a valid termcap file" unless @termcap_path || $entry;
  140.  
  141.     $state = 1;                    # 0 == finished
  142.  
  143.     $first = 0;                    # first entry (keeps term name)
  144.  
  145.     $max = 32;                    # max :tc=...:'s
  146.  
  147.     if ($entry) {
  148.     $first++;                # we're the first entry
  149.     if ($entry =~ s/:tc=([^:]+):/:/) {
  150.         $tmp_term = $1;
  151.         $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g;
  152.     }
  153.     else {
  154.         $state = 0;                # we're already finished
  155.     }
  156.     }
  157.  
  158.     $search = q{
  159.     while (<TERMCAP>) {
  160.         next if /^\\t/ || /^#/;
  161.         if ($_ =~ m/(^|\\|)${termpat}[:|]/o) {
  162.         chomp;
  163.         s/^[^:]*:// if $first++;
  164.         $state = 0;
  165.         while ($_ =~ s/\\\\$//) {
  166.             defined(my $x = <TERMCAP>) or last;
  167.             $_ .= $x; chomp;
  168.         }
  169.         last;
  170.         }
  171.     }
  172.     defined $entry or $entry = '';
  173.     $entry .= $_;
  174.     };
  175.  
  176.     while ($state != 0) {
  177.     if ($state == 1) {
  178.         $TERMCAP = shift @termcap_path
  179.         || croak "failed termcap lookup on $tmp_term";
  180.     }
  181.     else {
  182.         $max-- || croak "failed termcap loop at $tmp_term";
  183.         $state = 1;        # ok, maybe do a new file next time
  184.     }
  185.  
  186.     open(TERMCAP,"< $TERMCAP\0") || croak "open $TERMCAP: $!";
  187.     eval $search;
  188.     die $@ if $@;
  189.     close TERMCAP;
  190.  
  191.     $entry =~ s/:tc=([^:]+):/:/ && ($tmp_term = $1, $state = 2);
  192.     $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g;
  193.     }
  194.  
  195.     croak "Can't find $term" if $entry eq '';
  196.     $entry =~ s/:+\s*:+/:/g;                # cleanup $entry
  197.     $entry =~ s/:+/:/g;                    # cleanup $entry
  198.     $self->{TERMCAP} = $entry;                # save it
  199.  
  200.     $entry =~ s/^[^:]*://;
  201.     foreach $field (split(/:[\s:\\]*/,$entry)) {
  202.     if ($field =~ /^(\w\w)$/) {
  203.         $self->{'_' . $field} = 1 unless defined $self->{'_' . $1};
  204.     }
  205.     elsif ($field =~ /^(\w\w)\@/) {
  206.         $self->{'_' . $1} = "";
  207.     }
  208.     elsif ($field =~ /^(\w\w)#(.*)/) {
  209.         $self->{'_' . $1} = $2 unless defined $self->{'_' . $1};
  210.     }
  211.     elsif ($field =~ /^(\w\w)=(.*)/) {
  212.         next if defined $self->{'_' . ($cap = $1)};
  213.         $_ = $2;
  214.         s/\\E/\033/g;
  215.         s/\\(\d\d\d)/pack('c',oct($1) & 0177)/eg;
  216.         s/\\n/\n/g;
  217.         s/\\r/\r/g;
  218.         s/\\t/\t/g;
  219.         s/\\b/\b/g;
  220.         s/\\f/\f/g;
  221.         s/\\\^/\377/g;
  222.         s/\^\?/\177/g;
  223.         s/\^(.)/pack('c',ord($1) & 31)/eg;
  224.         s/\\(.)/$1/g;
  225.         s/\377/^/g;
  226.         $self->{'_' . $cap} = $_;
  227.     }
  228.     }
  229.     $self->{'_pc'} = "\0" unless defined $self->{'_pc'};
  230.     $self->{'_bc'} = "\b" unless defined $self->{'_bc'};
  231.     $self;
  232. }
  233.  
  234. sub Tpad { ## public
  235.     my $self = shift;
  236.     my($string, $cnt, $FH) = @_;
  237.     my($decr, $ms);
  238.  
  239.     if ($string =~ /(^[\d.]+)(\*?)(.*)$/) {
  240.     $ms = $1;
  241.     $ms *= $cnt if $2;
  242.     $string = $3;
  243.     $decr = $self->{PADDING};
  244.     if ($decr > .1) {
  245.         $ms += $decr / 2;
  246.         $string .= $self->{'_pc'} x ($ms / $decr);
  247.     }
  248.     }
  249.     print $FH $string if $FH;
  250.     $string;
  251. }
  252.  
  253. sub Tputs { ## public
  254.     my $self = shift;
  255.     my($cap, $cnt, $FH) = @_;
  256.     my $string;
  257.  
  258.     if ($cnt > 1) {
  259.     $string = Tpad($self, $self->{'_' . $cap}, $cnt);
  260.     } else {
  261.     $string = defined $self->{$cap} ? $self->{$cap} :
  262.         ($self->{$cap} = Tpad($self, $self->{'_' . $cap}, 1));
  263.     }
  264.     print $FH $string if $FH;
  265.     $string;
  266. }
  267.  
  268. sub Tgoto { ## public
  269.     my $self = shift;
  270.     my($cap, $code, $tmp, $FH) = @_;
  271.     my $string = $self->{'_' . $cap};
  272.     my $result = '';
  273.     my $after = '';
  274.     my $online = 0;
  275.     my @tmp = ($tmp,$code);
  276.     my $cnt = $code;
  277.  
  278.     while ($string =~ /^([^%]*)%(.)(.*)/) {
  279.     $result .= $1;
  280.     $code = $2;
  281.     $string = $3;
  282.     if ($code eq 'd') {
  283.         $result .= sprintf("%d",shift(@tmp));
  284.     }
  285.     elsif ($code eq '.') {
  286.         $tmp = shift(@tmp);
  287.         if ($tmp == 0 || $tmp == 4 || $tmp == 10) {
  288.         if ($online) {
  289.             ++$tmp, $after .= $self->{'_up'} if $self->{'_up'};
  290.         }
  291.         else {
  292.             ++$tmp, $after .= $self->{'_bc'};
  293.         }
  294.         }
  295.         $result .= sprintf("%c",$tmp);
  296.         $online = !$online;
  297.     }
  298.     elsif ($code eq '+') {
  299.         $result .= sprintf("%c",shift(@tmp)+ord($string));
  300.         $string = substr($string,1,99);
  301.         $online = !$online;
  302.     }
  303.     elsif ($code eq 'r') {
  304.         ($code,$tmp) = @tmp;
  305.         @tmp = ($tmp,$code);
  306.         $online = !$online;
  307.     }
  308.     elsif ($code eq '>') {
  309.         ($code,$tmp,$string) = unpack("CCa99",$string);
  310.         if ($tmp[$[] > $code) {
  311.         $tmp[$[] += $tmp;
  312.         }
  313.     }
  314.     elsif ($code eq '2') {
  315.         $result .= sprintf("%02d",shift(@tmp));
  316.         $online = !$online;
  317.     }
  318.     elsif ($code eq '3') {
  319.         $result .= sprintf("%03d",shift(@tmp));
  320.         $online = !$online;
  321.     }
  322.     elsif ($code eq 'i') {
  323.         ($code,$tmp) = @tmp;
  324.         @tmp = ($code+1,$tmp+1);
  325.     }
  326.     else {
  327.         return "OOPS";
  328.     }
  329.     }
  330.     $string = Tpad($self, $result . $string . $after, $cnt);
  331.     print $FH $string if $FH;
  332.     $string;
  333. }
  334.  
  335. sub Trequire { ## public
  336.     my $self = shift;
  337.     my($cap,@undefined);
  338.     foreach $cap (@_) {
  339.     push(@undefined, $cap)
  340.         unless defined $self->{'_' . $cap} && $self->{'_' . $cap};
  341.     }
  342.     croak "Terminal does not support: (@undefined)" if @undefined;
  343. }
  344.  
  345. 1;
  346.  
  347.